<?php

namespace App\Http\Controllers;
use App\Tool\Error;
use Laravel\Lumen\Routing\Controller as BaseController;

class Controller extends BaseController
{
    /**
     * 返回错误
     * @param $errorCode
     * @param null $errorMsg
     * @return \Illuminate\Http\JsonResponse
     */
    protected function error($errorCode, $errorMsg = null)
    {
        if ($errorMsg === null) {
            $errorMsg = empty(Error::MSG[$errorCode]) ? '未定义错误 ' . $errorCode : Error::MSG[$errorCode];
        }
        $result = array(
            'code' => $errorCode,
            'msg' => $errorMsg
        );
        return response()->json($result);
    }

    /**
     * 返回成功
     * @param $message
     * @param null $data
     * @return \Illuminate\Http\JsonResponse
     */
    protected function success($message = '操作成功', $data = null, $start = null, $end = null)
    {
        $res = array(
            'code' => 0,
            'msg' => $message,
        );
        if (isset($data)) {
            $res['data'] = $data;
        }
        if (isset($start)) {
            $res['start'] = $start;
        }
        if (isset($end)) {
            $res['end'] = $end;
        }
        return response()->json($res);
    }
}
